package com.wwwarehouse.contract.adjust_contract;


import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.DialogTools;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.ConfirmationEvent;

import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Creator by zhenwang.xiang on 2017/10/23 10:13
 * Description: 二次确认界面
 */


public class SecondaryConfirmationFragment extends BaseFragment implements View.OnClickListener {

    private TextView mBuName;
    private Button mBtnConsidering, mBtnConfirmTermination;
    private String mContractUkid;
    private String businessId;
    private String businessUnitId;
    private String businessUnitName;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_secondary_confirmation, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        requestHttp();
    }

    private void initView() {
        mBuName = $(R.id.tv_bu_name);
        mBtnConsidering = $(R.id.btn_considering);
        mBtnConfirmTermination = $(R.id.btn_confirm_termination);

        if (getArguments() != null) {
            mContractUkid = getArguments().getString("contractUkid");
            businessId = getArguments().getString("businessId");
        }

        mBtnConsidering.setOnClickListener(this);
        mBtnConfirmTermination.setOnClickListener(this);
    }

    private void requestHttp() {
        Map<String, Object> map = new HashMap<>();
        map.put("contractUkid", mContractUkid);
        map.put("businessId", businessId);
        NoHttpUtils.httpPost(ContractConstant.GET_INFLUENCE_TASK, map, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            if ("0".equals(commonClass.getCode())) {
                switch (what) {
                    case 0:
                        if (commonClass.getData() != null) {
                            JSONObject object = JSON.parseObject(commonClass.getData().toString());
                            businessUnitId = object.getString("businessUnitId");
                            businessUnitName = object.getString("businessUnitName");
                            mBuName.setText("终止订单造成的影响将由" + businessUnitName + "承担；");
                        }
                        break;
                    case 1:
                        toast("已终止");
                        popFragment();
                        popFragment();
                        EventBus.getDefault().post(new ConfirmationEvent("refresh"));
                        break;
                }
            } else if ("703038".equals(commonClass.getCode())) {
                DialogTools.getInstance().showCustomWarningNoTitle(getActivity(), commonClass.getMsg(), "确定", true, new DialogTools.ConfirmListener() {
                    @Override
                    public void setConfirmListener(Dialog dialog, View view, String s) {
                        dialog.dismiss();
                        popFragment();
                        popFragment();
                        EventBus.getDefault().post(new ConfirmationEvent("refresh"));
                    }
                });
            } else {
                toast(commonClass.getMsg());
            }
        }

        @Override
        public void onFailed(String result, int what) {
        }

        @Override
        public void onFinish(int what) {

        }
    };

    @Override
    public void setTitle() {
        if (peekFragment() instanceof SecondaryConfirmationFragment) {
            mActivity.setTitle(getString(R.string.secondary_confrimation_title));
        }
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_confirm_termination) {
            Map<String, Object> map = new HashMap<>();
            map.put("contractUkid", mContractUkid);
            map.put("stopName", businessUnitName);
            map.put("stopId", businessUnitId);
            NoHttpUtils.httpPost(ContractConstant.TERMINATION_ORDER, map, mOnResponseListener, 1);
        } else if (i == R.id.btn_considering) {
            popFragment();
        }
    }
}
